package com.wocao.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.wocao.entity.SysUser;
import com.wocao.service.SysUserService;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * sysUser导入监听器
 */
public class SysUserReadListener implements ReadListener<SysUser> {

    List<SysUser> userList = new ArrayList<>();

    private SysUserService sysUserService;

    public SysUserReadListener(SysUserService sysUserService){
        this.sysUserService = sysUserService;
    }

    //读一行触发一次
    @Override
    public void invoke(SysUser sysUser, AnalysisContext analysisContext) {
        System.out.println("===读取到===>"+sysUser);
        userList.add(sysUser);
    }

    //读完触发一次
    @Transactional
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("======>读取完毕");
        sysUserService.saveOrUpdateBatch(userList);
    }
}
